package com.minazuki.signinserver.service;

import com.minazuki.signinserver.config.JwtProperties;
import com.minazuki.signinserver.pojo.api.UserInfo;
import com.minazuki.signinserver.pojo.dao.*;
import com.minazuki.signinserver.utils.JwtUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.util.Sqls;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class SigninService {
    private static final Logger logger = LoggerFactory.getLogger(SigninService.class);

    @Autowired
    SigninRecordMapper mapper;
    @Autowired
    UserMapper userMapper;
    @Autowired
    CourseMapper courseMapper;
    @Autowired
    CourseSelectMapper courseSelectMapper;
    @Autowired
    CourseService courseService;
    @Autowired
    PublishRecordMapper publishRecordMapper;
    @Autowired
    JwtProperties jwtProperties;

    public boolean addSign(int pid, String token) {
        UserInfo info;
        try {
            info = JwtUtils.getInfoFromToken(token, jwtProperties.getPublicKey());
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }

        PublishRecord record = new PublishRecord(pid);
        record = publishRecordMapper.selectByPrimaryKey(record);

        if (record == null) {//判断签到存在
            logger.info("签到不存在");
            return false;
        }
        int cid = record.getCid();
        CourseSelect courseSelect = new CourseSelect();
        courseSelect.setUid(info.getId());
        courseSelect.setCid(cid);
        if (courseSelectMapper.selectCount(courseSelect) == 0) {//判断用户是否在课程里
            logger.info("用户" + info.getUsername() + "不在这个课程里");
            return false;
        }
        if (record.getEndTime().before(new Date())) {
            logger.info("签到" + record.getPid() + "已经过期");
            return false;
        }
        SigninRecord newRecord = new SigninRecord();
        newRecord.setPid(pid);
        newRecord.setUid(info.getId());

        if (mapper.selectCount(newRecord) > 0) { //判断已签到
            logger.info("用户已签到");
            return false;
        }
        newRecord.setUsername(info.getUsername());
        newRecord.setTime(new Timestamp(new Date().getTime()));

        return mapper.insertSelective(newRecord) > 0;
    }

    public List getSignedUser(int pid, String token) {
        UserInfo info;
        try {
            info = JwtUtils.getInfoFromToken(token, jwtProperties.getPublicKey());
        } catch (Exception e) {
            e.printStackTrace();
            return new ArrayList();
        }
        PublishRecord p = new PublishRecord();
        p.setPid(pid);
        p = publishRecordMapper.selectOne(p);
        Course course = new Course();
        course.setUid(info.getId());
        course.setId(p.getCid());

        CourseSelect courseSelect = new CourseSelect();
        courseSelect.setUid(info.getId());
        courseSelect.setCid(p.getCid());
        if (courseMapper.selectCount(course) == 0 && courseSelectMapper.selectCount(courseSelect) == 0)//判断用户是否在这个课程里
            return new ArrayList();

        Example example = new Example.Builder(SigninRecord.class)
                .where(Sqls.custom().andEqualTo("pid", pid))
                .build();
        List<SigninRecord> result = mapper.selectByExample(example);
        for (int i = 0; i < result.size(); i++) {
            User u = new User();
            u.setId(result.get(i).getUid());
            u = userMapper.selectOne(u);
            result.get(i).setUsername(u.getUsername());
        }
        return result;
    }

    public boolean checkSign(int pid, String token) {
        UserInfo info;
        try {
            info = JwtUtils.getInfoFromToken(token, jwtProperties.getPublicKey());
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        SigninRecord record = new SigninRecord();
        record.setUid(info.getId());
        record.setPid(pid);
        return mapper.selectCount(record) > 0;
    }
}
